package com.zhong.gang.app.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.luck.picture.lib.tools.ToastUtils;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.zhong.gang.app.R;
import com.zhong.gang.app.databinding.ActivityMainBinding;
import com.zhong.gang.applib.base.act.ActBaseRefresh;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.modle.ListBean;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import io.reactivex.functions.Consumer;

public class MainActivity extends ActBaseRefresh<ActivityMainBinding,MainAdapter>
        implements MainController.MainListener{
    private static final int SCAN = 100;
    private MainPresenter presenter;
    public static final String MAIN_ADD_OK = "MAIN_ADD_OK";
    private MainController mainController;

    @NonNull
    @Override
    public String CreateTitle() {
        return "首页";
    }


    @Override
    protected boolean neeBack() {
        return false;
    }
    @Override
    public int LayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        bind.setAct(this);
        mainController = new MainController(this,this);
        mainController.initTypePop(bind.typeLayout,bind.typeText,bind.typeImg,bind.newOrderTv);
        mainController.initStatusPop(bind.statusLayout,bind.statusText,bind.statusImg);
        mainController.initIdentity(bind.examine,bind.scan,bind.newOrder);
        mainController.initLeftView(getTitleBar());
        mainController.initRightView(getTitleBar());
        initRx();
        bind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initrefrsh(bind.pullto,new MainAdapter(),bind.recyclerView);
        adapter.setOnItemClickListener(mainController);
        bind.setControll(mainController);
        presenter = new MainPresenter();
        getData();
    }


    /**
     * 初始话监听事件
     */
    private void initRx(){
        getRxManager().add(MAIN_ADD_OK, new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                onRefresh();
            }
        });
    }


    @Override
    public void getData() {
        super.getData();
        presenter.getList(this,page,bind.searchEdt.getText().toString()
                ,mainController.getType(),mainController.getStatus(),new ActCallBack<BaseDataBean<ListBean<MainBean>>>(){
            @Override
            public void onResponse(BaseDataBean<ListBean<MainBean>> data) throws Exception {
                super.onResponse(data);
                setData(data.getResult().getRecords());
            }

            @Override
            public void onError(String str) {
                showMainFragemt();
                setData(new ArrayList<MainBean>());
            }
        });
        mainController.getMsgUnReadCount(this,presenter);
    }


    @Override
    protected void onResume() {
        super.onResume();
        mainController.getMsgUnReadCount(this,presenter);
    }



    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.type_layout:
                mainController.showPop(bind.typeImg,bind.typeLayout,mainController.getTypePop(),mainController.getType());
                break;
            case R.id.status_layout:
                mainController.showPop(bind.statusImg,bind.statusLayout,mainController.getStatusPop(),mainController.getStatus());
                break;
            case R.id.find:
                onRefresh();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK || data == null)
            return;
        if(requestCode == SCAN){
            Bundle bundle = data.getExtras();
            if (bundle == null) {
                return;
            }
            if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                String result = bundle.getString(CodeUtils.RESULT_STRING);
                try {
                    JSONObject object = new JSONObject(result);
                    mainController.setType(object.getInt("type"),object.getInt("id"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                ToastUtils.s(this, "解析二维码失败");
            }
        }
    }
}
